//
//  IMImageMessageTableViewCell.m
//  markmap
//
//  Created by 甘世清 on 2024/5/28.
//

#import "IMImageMessageTableViewCell.h"

@implementation IMImageMessageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    [self.contentView addSubview:self.imImageView];
    [self.imImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarImageView);
        make.left.equalTo(self.contentView).offset(12+48+10);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SQ_imageViewClick)];
    [self.imImageView addGestureRecognizer:tap];
    
    UIView * hudView = [[UIView alloc]init];
    hudView.backgroundColor = UIColor.clearColor;
    hudView.userInteractionEnabled = NO;
    [self.imImageView addSubview:hudView];
    [hudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.equalTo(self.imImageView);
        make.size.mas_equalTo(34);
    }];
    [hudView addSubview:self.hud];
    [self.contentView addSubview:self.blackImageView];
    [self.blackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(24);
        make.right.equalTo(self.imImageView.mas_left).offset(-4);
        make.bottom.equalTo(self.imImageView.mas_bottom);
    }];
}
- (void)setModel:(IMMessageModel *)model{
    [super setModel:model];
    [self.imImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (model.sendType) {
            make.left.equalTo(self.contentView).offset(12+48+10);
        } else {
            make.left.equalTo(self.contentView).offset(SCREEN_WIDETH-12-48-10-model.cellSize.width);
        }
        make.width.mas_equalTo(model.cellSize.width);
        make.height.mas_equalTo(model.cellSize.height);
    }];
//    [self.imImageView sd_setImageWithURL:SQURLImage(model.content)];
    WS(weakSelf);
    [self.hud startAnimating];
    [self.imImageView sd_setImageWithURL:SQURLImage(model.content) completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        [weakSelf.hud stopAnimating];
    }];
    [self.contentView layoutIfNeeded];
}
-(void)SQ_imageViewClick{
    if (self.imageBlock) {
        self.imageBlock(self.imImageView);
    }  
}
- (UIImageView *)imImageView{
    if (!_imImageView) {
        _imImageView = [[UIImageView alloc]init];
        _imImageView.contentMode = UIViewContentModeScaleAspectFit;
        _imImageView.layer.cornerRadius = 6;
        _imImageView.layer.masksToBounds = YES;
        _imImageView.userInteractionEnabled = YES;
        _imImageView.backgroundColor = SQHexColor(@"#FFFFFF", 0.4);
        _imImageView.layer.borderColor = SQHexColor(@"#FFFFFF", 0.4).CGColor;
        _imImageView.layer.borderWidth = 0.4;
    }
    return _imImageView;
}

- (UIActivityIndicatorView *)hud{
    if (!_hud) {
        _hud = [[UIActivityIndicatorView  alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
        _hud.hidesWhenStopped = YES;
        _hud.center = CGPointMake(17, 17);
        _hud.color = UIColor.whiteColor;
    }
    return _hud;
}
@end
